﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MyOA.BLL;
using MyOA.Model;

namespace MyOA.UI.File.FileManage
{
    public partial class FileMain : System.Web.UI.Page
    {
        FileMainManage manage = new FileMainManage();
        public int FileId
        {
            set { ViewState["fileId"] = value; }
            get
            {
                try
                {
                    return (int)ViewState["fileId"];
                }
                catch (Exception)
                {

                    return 0;
                }
            }
        }
        private void dataBind(IList<FileInfo> fileInfos)
        {
            rpFileInfo.DataSource = fileInfos;
            rpFileInfo.DataBind();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["loginId"] == null)
            {
                Response.Redirect("~/Login.aspx");
                return;
            }

            if (!IsPostBack)
            {
                FileId = -1;
                if (Request.QueryString["fileId"] != null)
                {
                    FileId = int.Parse(Request.QueryString["fileId"]);

                    IList<FileInfo> fileInfos = manage.getFileInfoByParentId(FileId);
                    if (fileInfos.Count > 0)
                    {
                        dataBind(fileInfos);
                    }
                    string filePath = manage.getFilePathByFileId(FileId);
                    txtAddress.Text = filePath;
                }
                else
                {
                    txtAddress.Text = "c://";
                    IList<FileInfo> fileInfos = manage.getFileInfoByParentId(0);
                    if (fileInfos.Count > 0)
                    {
                        dataBind(fileInfos);
                    }
                }
            }

        }

        /// <summary>
        /// 删除按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtnDelete_Click(object sender, EventArgs e)
        {
            int fileId = int.Parse(((LinkButton)sender).CommandName);
            if (manage.updateIfDeleteByFileId(fileId))
            {
                string script = string.Format("<script>Alert('删除成功',null,null,'温馨提示');parent.document.getElementById('ifrFileLeft').src='FileLeft.aspx?id={0}'</script>", DateTime.Now.Ticks.ToString());
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", script, false);
                IList<FileInfo> fileInfos = manage.getFileInfoByParentId(FileId);
                if (fileInfos.Count > 0)
                {
                    dataBind(fileInfos);
                }
                else
                {
                    rpFileInfo.DataSource = null;
                    rpFileInfo.DataBind();
                }
            }
            else
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", "<script>Alert('删除失败',null,null,'温馨提示')</script>", false);
            }







        }

        /// <summary>
        /// repeater控件中的详细
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtnDetail_Click(object sender, EventArgs e)
        {
            int fileId = int.Parse(((LinkButton)sender).CommandName);
            int fileTypeId = int.Parse(((LinkButton)sender).CommandArgument);
            if (fileTypeId == 1)
            {
                Response.Redirect("OperateFolder.aspx?fileId=" + fileId);
            }
            else
            {
                Response.Redirect("OperateFile.aspx?fileId=" + fileId);
            }
        }

        /// <summary>
        /// 返回到跟目录的按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgbtnUp_Click(object sender, ImageClickEventArgs e)
        {

            txtAddress.Text = "c://";
            IList<FileInfo> fileInfos = manage.getFileInfoByParentId(0);
            if (fileInfos.Count > 0)
            {
                dataBind(fileInfos);
            }
            else
            {
                rpFileInfo.DataSource = null;
                rpFileInfo.DataBind();
            }

            FileId = -1;
        }

        /// <summary>
        /// 新建文件夹按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgbtnNewFolder_Click(object sender, ImageClickEventArgs e)
        {
            if (FileId == -1)
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", "<script>Alert('请你选择要添加文件夹的目录',null,null,'温馨提示')</script>", false);
                return;
            }
            int fileTypeId = manage.getFileTypeIdByFileId(FileId);
            if (fileTypeId != 1)
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", "<script>Alert('只能在文件夹下创建',null,null,'温馨提示')</script>", false);
                return;
            }
            Response.Redirect("~/File/FileManage/OperateFolder.aspx?path=" + Server.UrlEncode(txtAddress.Text)+"&parentId="+FileId);
        }

        /// <summary>
        /// 新建文件按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgbtnNewFile_Click(object sender, ImageClickEventArgs e)
        {
            if (FileId == -1)
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", "<script>Alert('请你选择要添加文件夹的目录',null,null,'温馨提示')</script>", false);
                return;
            }
            int fileTypeId = manage.getFileTypeIdByFileId(FileId);
            if (fileTypeId != 1)
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", "<script>Alert('只能在文件夹下创建',null,null,'温馨提示')</script>", false);
                return;
            }
            Response.Redirect("~/File/FileManage/OperateFile.aspx?path=" + Server.UrlEncode(txtAddress.Text) + "&parentId=" + FileId);
        }
    }
}
